import React, { useRef } from 'react';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { getList, setStatus, del, delAll } from '@/services/toolAdmin/user/user/api';
import { Switch, Space, message, Image } from 'antd';
import { PageContainer } from '@ant-design/pro-layout';
import { exportExcel } from '@/utils/ExportExcel';
import { getSort, setLsetData } from '@/utils/utils';
import FormIndex from './components/FormIndex';
import CDel from '@/components/common/CDel';
import CDelAll from '@/components/common/CDelAll';
const Index: React.FC = () => {
  const actionRef = useRef<ActionType>();
  let maps = {};
  const columns: ProColumns<API.ToolAdminUserList>[] = [
    {
      title: '编号',
      dataIndex: 'id',
      hideInSearch: true,
      align: 'center',
      width: 80,
      fixed: true,
    },
    {
      title: '上级用户ID',
      dataIndex: 'pid',
      align: 'center',
      width: 100,
      ellipsis: true,
      copyable: true,
    },
    {
      title: '积分',
      dataIndex: 'integral',
      align: 'center',
      width: 100,
    },
    {
      title: '昵称',
      dataIndex: 'nick_name',
      align: 'center',
      width: 200,
      ellipsis: true,
      copyable: true,
    },
    {
      title: '头像',
      dataIndex: 'avatar_url',
      hideInSearch: true,
      width: 100,
      align: 'center',
      render: (text, record) => [
        <Image
          width={20}
          key={record.id}
          src={record.avatar_url}
          fallback=""
        />,
      ],
    },
    {
      title: '姓名',
      dataIndex: 'name',
      align: 'center',
      width: 200,
      ellipsis: true,
      copyable: true,
    },
    {
      title: '电话',
      dataIndex: 'mobile',
      align: 'center',
      width: 200,
      ellipsis: true,
      copyable: true,
    },
    {
      title: '顶级业务员',
      dataIndex: 'status',
      valueType: 'select',
      valueEnum: {
        0: {
          text: '否',
          status: '0',
        },
        1: {
          text: '是',
          status: '1',
        },
      },
      hideInTable: true,
    },
    {
      title: '顶级业务员',
      dataIndex: 'status',
      hideInSearch: true,
      width: 100,
      align: 'center',
      render: (text, record) => [
        <Switch
          key={record.id + record.status}
          checkedChildren="是"
          unCheckedChildren="否"
          defaultChecked={record.status === 1 ? true : false}
          onChange={async (checked: boolean) => {
            const res = await setStatus(record.id, { status: checked ? 1 : 0 });
            if (res.status === 20000) {
              message.success(res.message);
            } else {
              actionRef.current?.reload();
            }
          }}
        />,
      ],
    },
    {
      title: '会员类型',
      dataIndex: 'type',
      valueType: 'select',
      align: 'center',
      width: 150,
      valueEnum: {
        1: {
          text: '普通用户',
          status: '1',
        },
        2: {
          text: '业务员',
          status: '2',
        },
      },
    },
    {
      title: '用户渠道',
      dataIndex: 'open',
      valueType: 'select',
      align: 'center',
      width: 150,
      valueEnum: {
        1: {
          text: '微信',
          status: '1',
        },
        2: {
          text: '抖音',
          status: '2',
        },
      },
    },
    {
      title: '创建时间',
      dataIndex: 'created_at',
      key: 'created_at',
      valueType: 'dateTime',
      sorter: true,
      hideInSearch: true,
      width: 180,
      align: 'center',
    },
    {
      title: '创建时间',
      key: 'created_at',
      dataIndex: 'created_at',
      valueType: 'dateTimeRange',
      hideInTable: true,
      search: {
        transform: (value) => {
          return {
            'created_at[0]': value[0],
            'created_at[1]': value[1],
          };
        },
      },
    },
    {
      title: '更新时间',
      key: 'updated_at',
      dataIndex: 'updated_at',
      valueType: 'dateTime',
      sorter: true,
      hideInSearch: true,
      width: 180,
      align: 'center',
    },
    {
      title: '更新时间',
      key: 'updated_at',
      dataIndex: 'updated_at',
      valueType: 'dateTimeRange',
      hideInTable: true,
      search: {
        transform: (value) => {
          return {
            updated_at: value,
          };
        },
      },
    },
    {
      title: '操作',
      key: 'option',
      width: 160,
      valueType: 'option',
      align: 'center',
      fixed: 'right',
      render: (_, row) => [
        <FormIndex
          id={row.id}
          key={row.id}
          onConfirm={() => {
            return actionRef.current?.reload();
          }}
        />,
        <CDel
          key={row.id}
          onCancel={async () => {
            const res = await del(row.id);
            if (res.status === 20000) {
              message.success(res.message);
              actionRef.current?.reload();
            }
          }}
        />,
      ],
    },
  ];
  return (
    <PageContainer fixedHeader={true} title={false}>
      <ProTable
        scroll={{ x: 1300 }}
        actionRef={actionRef}
        rowKey="id"
        columns={columns}
        request={async (params: API.ListQequest, sort) => {
          const data: any = await getSort(params, sort);
          const res = await getList(data);
          return setLsetData(res);
        }}
        options={{ fullScreen: true }}
        rowSelection={{ fixed: true }}
        tableAlertRender={({ selectedRowKeys, onCleanSelected }) => (
          <Space size={24}>
            <span>
              已选 {selectedRowKeys.length} 项
              <a style={{ marginLeft: 8 }} onClick={onCleanSelected}>
                取消选择
              </a>
            </span>
          </Space>
        )}
        columnsState={{
          onChange: (map) => {
            maps = map;
          },
        }}
        tableAlertOptionRender={({ selectedRows, selectedRowKeys, onCleanSelected }) => {
          return (
            <Space size={16}>
              <CDelAll
                key={selectedRowKeys.length}
                count={selectedRowKeys.length}
                onCancel={async () => {
                  const res = await delAll({ idArr: selectedRowKeys });
                  if (res.status === 20000) {
                    message.success(res.message);
                    actionRef.current?.reload();
                    onCleanSelected();
                  }
                }}
              />
              <a
                onClick={async () => {
                  const selectedRowsCope = JSON.parse(JSON.stringify(selectedRows));
                  selectedRowsCope.map((item: any) => {
                    item.type = ['普通用户', '业务员'][item.type - 1];
                    item.open = ['微信', '抖音'][item.open - 1];
                    item.status = ['否', '是'][item.status - 1];
                    return item;
                  });
                  exportExcel({
                    fileName: '会员管理',
                    columns,
                    maps,
                    selectedRows: selectedRowsCope,
                  });
                }}
              >
                导出数据
              </a>
            </Space>
          );
        }}
      />
    </PageContainer>
  );
};
export default Index;
